<?php
namespace app\modules\supplier\logic;
use app\modules\base\LogicBase;
use think\Db;
/**
 *
 */

class GoodsApplyLogic extends LogicBase
{

    //验证
    protected $valid;

    public function _initialize()
    {

        $this->valid = validate('app\modules\supplier\validate\GoodsApplyPost'); //游客验证
    }
    /**
     * 添加供应商申请
     * @param array $data 参数
     * @return bool|array
     */

    public function add($data = [])
    {

        // Db::startTrans();//开启事物

        if (!$this->valid->check($data)) {//字段验证
            $this->_error[] = $this->valid->getError();
            return false;
        }else{
            //避免重复申请 未审核情况
            if (Db::name('goods_apply')->where(['apply_title'=>$data['apply_title'],'supplier_id'=>$data['supplier_id'],'apply_status'=>1])->find()) {
                $this->_error[] ='已经申请过了，请等待审核！';
                return false;
            }
            //避免重复申请 审核通过情况
            if (Db::name('goods_apply')->where(['apply_title'=>$data['apply_title'],'supplier_id'=>$data['supplier_id'],'apply_status'=>2])->find()) {
                $this->_error[] ='申请已经通过！';
                return false;
            }

        }


        $res = Db::name('goods_apply')->insert($data);

        if ($res) {
            return true;
        }else{
            return false;
        }

    }
}